package com.zb.config;

import cn.dev33.satoken.exception.NotLoginException;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.zb.exception.LoginException;
import com.zb.exception.RegisterException;
import com.zb.exception.SMSException;
import com.zb.result.Result;
import com.zb.result.ResultCode;
import lombok.extern.log4j.Log4j2;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 全局异常处理配置类
 * @Author WEN
 * @Date 2023/7/7 0007
 **/
@Log4j2
@ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandlerConfig {

    /**
     * 空值或空对象类异常
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e) {
        return Result.error(ResultCode.NULL.getCode(), e.getMessage());
    }

    /**
     * mybatis-plus 异常提示
     * 空值或空对象类异常
     * @param e
     * @return
     */
    @ExceptionHandler(MybatisPlusException.class)
    public Result mybatisPlusException(MybatisPlusException e) {
        return Result.error(ResultCode.NULL.getCode(), e.getMessage());
    }
    /**
     * 参数不合法异常
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Result illegalArgumentException(IllegalArgumentException e) {
        return Result.error(ResultCode.ILLEGAL_ARGUMENT.getCode(), e.getMessage());
    }

    /**
     * 参数内容值重复异常
     * @param e
     * @return
     */
    @ExceptionHandler(DuplicateKeyException.class)
    public Result DuplicateKeyException(DuplicateKeyException e) {
        return Result.error(ResultCode.ILLEGAL_PARAM_LENGTH.getCode(), "字段内容值重复提示：" + e.getCause().getMessage());
    }

    /**
     * 参数内容超长异常
     * @param e
     * @return
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result DataIntegrityViolationException(DataIntegrityViolationException e) {
        e.printStackTrace();
        if (e.getCause().getMessage().contains("doesn't have a default value")) {
            return Result.error(ResultCode.ILLEGAL_PARAM_LENGTH.getCode(), "必填字段内容为空提示：" + e.getCause().getMessage());
        }
        return Result.error(ResultCode.ILLEGAL_PARAM_LENGTH.getCode(), "字段内容长度超限提示：" + e.getCause().getMessage());
    }

    /**
     * 参数字段校验类异常
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result methodArgumentNotValidException(MethodArgumentNotValidException e) {
        return Result.error(ResultCode.ILLEGAL_ARGUMENT.getCode(), Objects.requireNonNull(e.getFieldError()).getDefaultMessage());
    }

    /**
     * 被占用、已存在类型异常
     * @param e
     * @return
     */
    @ExceptionHandler(RegisterException.class)
    public Result registerException(RegisterException e) {
        return Result.error(ResultCode.OCCUPANCY.getCode(), e.getMessage());
    }

    /**
     * 登录类异常
     * @param e
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public Result NotLoginException(NotLoginException e) {
        return Result.error(ResultCode.LOGIN.getCode(), "登录已过期，请重新登录");
    }

    /**
     * 登录类异常
     * @param e
     * @return
     */
    @ExceptionHandler(LoginException.class)
    public Result loginException(LoginException e) {
        return Result.error(ResultCode.LOGIN.getCode(), e.getMessage());
    }

    /**
     * 短信类异常
     * @param e
     * @return
     */
    @ExceptionHandler(SMSException.class)
    public Result SMSException(SMSException e) {
        return Result.error(ResultCode.SMS.getCode(), e.getMessage());
    }


    /**
     * 非法的无引号字符
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Result HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return Result.error(ResultCode.ILLEGAL_PARAM.getCode(), ResultCode.ILLEGAL_PARAM.getMessage() + "！原因："+ e.getMessage());
    }

    /**
     * 未知类异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result unknowException(Exception e) {
        log.error("出现未知异常：" + e);
        e.printStackTrace();
        return Result.error(ResultCode.UNKNOWN.getCode(), ResultCode.UNKNOWN.getMessage());
    }
}
